package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.NoticeChannelEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.FlowNoticeTemplateStatusVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowChannelRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.common.FlowCommonCodeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知渠道Controller
 *
 * @author wuliepeng
 * @date 2019-07-15
 */
@RestController
@RequestMapping("/notice/channel")
public class FlowNoticeChannelController {

    @Autowired
    private FlowChannelRepository flowChannelRepository;

    /**
     * 获取通知渠道类型列表
     *
     * @return
     */
    @GetMapping("/getNoticeChannelType")
    public ResponseResult getNoticeChannelType() {
        Map<String, String> codeToDesc = NoticeChannelEnum.getCodeToDesc();
        List<Map<String, String>> noticeChannelTypes = new ArrayList<>();
        for (Map.Entry<String, String> entry : codeToDesc.entrySet()) {
            Map<String, String> noticeChannelType = new HashMap<>(2);
            noticeChannelType.put("code", entry.getKey());
            noticeChannelType.put("name", entry.getValue());
            noticeChannelTypes.add(noticeChannelType);
        }
        return ResponseResult.success(noticeChannelTypes)
                .setTotalCount(noticeChannelTypes.size())
                .setPageSize(noticeChannelTypes.size());
    }

    /**
     * 渠道列表
     *
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody FlowChannelVo flowChannelVo) {
        return flowChannelRepository.list(flowChannelVo);
    }

    /**
     * 新增渠道
     *
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Validated FlowChannelAddVo flowChannelAddVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        String type = flowChannelAddVo.getChannelType();
        if (flowChannelRepository.isTemplateExist(type)) {
            return ResponseResult.fail(ErrorCode.CHANNEL_TYPE_EXIST.errCode(), ErrorCode.CHANNEL_TYPE_EXIST.errMessage());
        }
        return ResponseResult.success(flowChannelRepository.add(flowChannelAddVo));
    }

    /**
     * 编辑渠道
     *
     * @return
     */
    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody FlowChannelEditVo flowChannelEditVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        String type = flowChannelEditVo.getChannelType();
        if (flowChannelRepository.isTemplateExist(type)) {
            return ResponseResult.fail(ErrorCode.CHANNEL_TYPE_EXIST.errCode(), ErrorCode.CHANNEL_TYPE_EXIST.errMessage());
        }
        Integer updateRow = flowChannelRepository.edit(flowChannelEditVo);
        if (updateRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.UPDATE_FAILED.getCode(), FlowCommonCodeConstants.UPDATE_FAILED.getDesc());

    }

    /**
     * 渠道详情
     *
     * @return
     */
    @GetMapping("/detail")
    public ResponseResult detail(@RequestParam(value="id") String id) {
        FlowChannelInfo flowChannelInfo = flowChannelRepository.detail(id);
        return ResponseResult.success(flowChannelInfo);
    }


    /**
     * 删除渠道
     *
     * @return
     */
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam(value="id") String id) {
        Integer deleteRow = flowChannelRepository.delete(id);
        if (deleteRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.DELETE_FAILED.getCode(), FlowCommonCodeConstants.DELETE_FAILED.getDesc());
    }

    /**
     * 状态启用
     */
    @PostMapping("/startStatus")
    public ResponseResult startStatus(@RequestBody @Validated FlowChannelStatusVo flowChannelStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Integer row = flowChannelRepository.startStatus(flowChannelStatusVo);
        if (row > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.START_FAILED.getCode(), FlowCommonCodeConstants.START_FAILED.getDesc());
    }
}
